<?php

namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Session;

//大厅
class Order extends Controller
{

    //可接订单列表 带搜索
    public function getpageorderlist()
    {
        $page = input('page', 1);
        $pagesax = input('pagesax', 4);
        $seach = input('seach');
        $day = input('day');
        $money = input('money');
        $cost = input('cost');
        $company_type = input('company_type');
        $uid = 1;
        if ($seach != null) {
            $where['req_name'] = $seach;
        }

        if ($cost != null) {
            if ($cost == 1) {
                $where['sure_charge'] = array('elt', '10000');
            } elseif ($cost == 2) {
                $where['sure_charge'] = array(array('egt', '10000'), array('elt', '30000'), 'and');

            } elseif ($cost == 3) {
                $where['sure_charge'] = array(array('egt', '30000'), array('elt', '100000'), 'and');
            } elseif ($cost == 4) {
                $where['sure_charge'] = array('elt', '100000');
            }
        }

        if ($money != null) {
            if ($money == 1) {
                $where['face_amt'] = array('elt', '300000');
            } elseif ($money == 2) {
                $where['face_amt'] = array(array('egt', '300000'), array('elt', '1000000'), 'and');

            } elseif ($money == 3) {
                $where['face_amt'] = array(array('egt', '1000000'), array('elt', '2000000'), 'and');
            } elseif ($money == 4) {
                $where['face_amt'] = array('gt', '2000000');
            }
        }


        if ($company_type != null) {
            $where['company_type'] = $company_type;
        }
        $where['uid'] = ['<>', $uid];
        $where['status'] = 0;
        $where['valid'] = 1;
        $res = Db::name('bill_info')->where($where)->order('created_time desc')->select();
        //print_r($res);exit();
        foreach ($res as $key => &$value) {
            $time = time();
            $etime = $value['expirydate'];
            if ($etime < $time) {
                $days = ($time - $etime) / 86400;
                $value['days'] = ceil($days);
            } else {
                $value['days'] = 1;
            }
        }
        $list = [];
        if ($day != null) {
            foreach ($res as $k => &$v) {
                if ($day == 1) {
                    if ($v['days'] < 30 or $v['days'] == 30) {
                        $list[] = $v;
                    }
                }
                if ($day == 2) {
                    if ($v['days'] > 30 and $v['days'] < 90 or $v['days'] == 90) {
                        $list[] = $v;
                    }
                }
                if ($day == 3) {
                    if ($v['days'] > 90 and $v['days'] < 180) {
                        $list[] = $v;
                    }
                }
                if ($day == 4) {
                    if ($v['days'] > 180 or $v['days'] == 180) {
                        $list[] = $v;
                    }
                }
            }
            $view = page_array($pagesax, $page, $list, 1);
            return msg(1, '接单大厅首页数据', $view);
            exit();
        }
        $view = page_array($pagesax, $page, $res, 1);
        return msg(1, '接单大厅首页数据', $view);
        exit();
    }


    //查询保证金1000元
    public function getusermoney()
    {
        $uid = Session::get('uid');
        $where['id'] = $uid;
        $res = Db::name('user')->where($where)->find();
        if ($res['money'] < 1000) {
            return msg(2, '小于1000元保证金', $res);
        } else {
            return msg(1, '大于1000元', $res);
        }
    }


    //订单详情
    public function details()
    {
        $id = input('id');
        $res = Db::name('order_info')->alias('a')->join('bill_info b', 'a.b_id = b.id')->field('a.*,b.face_amt,b.expirydate,b.req_name,b.company_type')->where('a.id', $id)->find();
        $time = time();
        $etime = $res['expirydate'];
        if ($etime < $time) {
            $days = ($time - $etime) / 86400;
            $res['days'] = ceil($days);
        } else {
            $res['days'] = 1;
        }
        $res['bond'] = $res['service_charge'] * 0.1;

        return msg(1, '接单详情', $res);
    }





//    //卖方
//    public function  selllist(){
//        $sell_id =  input('sell_id');
//        $res = Db::name('order_info')->where('sell_id',$sell_id)->select();
//        return msg(1,'发布者列表',$res);
//
//    }
//
//    //买方
//    public function  buylist(){
//        $buy_id =  input('buy_id');
//        $res = Db::name('order_info')->where('buy_id',$buy_id)->select();
//        return msg(1,'接单者列表',$res);
//    }


//    //取消订单
//    public  function  editordercancel(){
//        $id = input('id');
//        $data['status'] = 4;
//        $data['cancel_time'] =time();
//        $res = Db::name('order_info')->where('id',$id)->update($data);
//        return msg(1,'取消订单',$res);
//    }
//    //服务完成
//    public  function  editorderconfirm(){
//        $id = input('id');
//        $data['status'] = 1;
//        $data['complete_time'] =time();
//        $res = Db::name('order_info')->where('id',$id)->update($data);
//        return msg(1,'服务完成',$res);
//    }
//
//    //改变订单状态
//    public function  editorder(){
//        $id = input('id');
//        $status = input('status');
//        $data['status'] = $status;
//        $res = Db::name('order_info')->where('id',$id)->update($data);
//        return msg(1,'操作成功',$res);
//    }


}
